package com.cskaoyan.controller.vx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.vxBo.*;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.wxPo.MarketUser;
import com.cskaoyan.bean.vo.AdminListVO;
import com.cskaoyan.bean.vo.WxTempVO;
import com.cskaoyan.bean.vo.vxVo.cartVo.*;
import com.cskaoyan.mapper.wxMapper.MarketCartMapper;
import com.cskaoyan.service.vx.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * ydp
 */

@RestController
@RequestMapping("wx/cart")
public class CartController {


    @Autowired
    CartService cartService;

    @Autowired
    MarketCartMapper marketCartMapper;



    @RequestMapping("goodscount")
    public BaseRespVo goodscount() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        Integer goodscount = 0;
        if (cartService.selectgoodscount(marketUser) != null) {
            goodscount = cartService.selectgoodscount(marketUser);
        }

        return BaseRespVo.ok(goodscount);
    }


    @RequestMapping("add")
    public BaseRespVo add(@RequestBody CartAddBo cartBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();


        if (cartService.selectAdded(cartBo, marketUser) != 0) {
            cartService.updateNumber(cartBo, marketUser);
        } else {
            cartService.insertINcart(cartBo, marketUser);
        }


        Integer goodscount = cartService.selectGoodsCountByUserId(marketUser.getId());

        return BaseRespVo.ok(goodscount);
    }

    @RequestMapping("index")
    public BaseRespVo index() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        List<Cartgoods> cartgoodsList = new ArrayList<Cartgoods>();
        if (cartService.showgoods(marketUser) != null) {
            cartgoodsList = cartService.showgoods(marketUser);
        }

        CartTotal cartTotal = cartService.showCartTotal(marketUser);
        CartIndexVo cartIndexVo = new CartIndexVo(cartTotal, cartgoodsList);

        return BaseRespVo.ok(cartIndexVo);
    }

    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedBo cartCheckedBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();


        cartService.updateCheckedCart(cartCheckedBo, cartCheckedBo.getProductIds(), marketUser);
        List<Cartgoods> cartgoodsList = cartService.showgoods(marketUser);
        CartTotal cartTotal = new CartTotal(0, 0, 0, 0);
        cartTotal = cartService.showCartTotal(marketUser);
        CartIndexVo cartIndexVo = new CartIndexVo(cartTotal, cartgoodsList);


        return BaseRespVo.ok(cartIndexVo);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CartDeleteBo cartDeleteBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        cartService.updateDeleteOneCart(cartDeleteBo.getProductIds(), marketUser);
        List<Cartgoods> cartgoodsList = new ArrayList<Cartgoods>();
        if (cartService.showgoods(marketUser) != null) {
            cartgoodsList = cartService.showgoods(marketUser);
        }

        CartTotal cartTotal = cartService.showCartTotal(marketUser);
        CartIndexVo cartIndexVo = new CartIndexVo(cartTotal, cartgoodsList);

        return BaseRespVo.ok(cartIndexVo);
    }

    @RequestMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody CartAddBo cartBo) {

        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();

        if (cartService.selectAdded(cartBo, marketUser) != 0) {
            cartService.updateNumber(cartBo, marketUser);
        } else {
            cartService.insertINcart(cartBo, marketUser);
        }


        Integer cartId = cartService.selectOneGoodsCount(cartBo, marketUser);

        return BaseRespVo.ok(cartId);


    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody CartUpdateBo cartUpdateBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        cartService.updateAddNumber(cartUpdateBo, marketUser);


        return BaseRespVo.ok(null);
    }


    @RequestMapping("checkout")
    public BaseRespVo checkout(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        CheckedAddress checkedAddress1 = cartService.queryAddressByAddressId(addressId);


        List<CheckedGoodsList> cartgoodsList = cartService.showCheckgoods(cartId);

        WxTempVO wxTempVO = new WxTempVO();
        Integer cartTotalPrice = cartService.selectCartTotalPrice(cartId);
        if (cartService.findDiscoutBycouponId(cartTotalPrice,marketUser.getId()) != null) {
            wxTempVO = cartService.findDiscoutBycouponId(cartTotalPrice ,marketUser.getId());
        }
        Integer actualPrice = cartTotalPrice - wxTempVO.getMin().intValue() ;


        CartCheckOutData cartCheckOutData = new CartCheckOutData(grouponRulesId, actualPrice, cartTotalPrice, cartId, wxTempVO.getCId(), wxTempVO.getUCid(), cartTotalPrice, addressId, 0, wxTempVO.getMin().intValue(), wxTempVO.getCount(), 0);
        cartCheckOutData.setCheckedAddress(checkedAddress1);
        cartCheckOutData.setCheckedGoodsList(cartgoodsList);


        return BaseRespVo.ok(cartCheckOutData);


    }


}
